home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 22 xml and adonet / xmldemo / xmldatadocumentform.vb < prev    next >
Text File  |  2002-03-16  |  8KB  |  205 lines

  1. Imports System.Data
  2. Imports System.Data.OleDb
  3. Imports System.Xml
  4.  
  5. Public Class XmlDataDocumentForm
  6.     Inherits System.Windows.Forms.Form
  7.  
  8. #Region " Windows Form Designer generated code "
  9.  
  10.     Public Sub New()
  11.         MyBase.New()
  12.  
  13.         'This call is required by the Windows Form Designer.
  14.         InitializeComponent()
  15.  
  16.         'Add any initialization after the InitializeComponent() call
  17.  
  18.     End Sub
  19.  
  20.     'Form overrides dispose to clean up the component list.
  21.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  22.         If disposing Then
  23.             If Not (components Is Nothing) Then
  24.                 components.Dispose()
  25.             End If
  26.         End If
  27.         MyBase.Dispose(disposing)
  28.     End Sub
  29.  
  30.     'Required by the Windows Form Designer
  31.     Private components As System.ComponentModel.IContainer
  32.  
  33.     'NOTE: The following procedure is required by the Windows Form Designer
  34.     'It can be modified using the Windows Form Designer.  
  35.     'Do not modify it using the code editor.
  36.     Friend WithEvents btnFill As System.Windows.Forms.Button
  37.     Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
  38.     Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
  39.     Friend WithEvents btnRefresh As System.Windows.Forms.Button
  40.     Friend WithEvents btnAddNode As System.Windows.Forms.Button
  41.     Friend WithEvents bthXPathSearches As System.Windows.Forms.Button
  42.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  43.         Me.btnFill = New System.Windows.Forms.Button()
  44.         Me.DataGrid1 = New System.Windows.Forms.DataGrid()
  45.         Me.TreeView1 = New System.Windows.Forms.TreeView()
  46.         Me.btnRefresh = New System.Windows.Forms.Button()
  47.         Me.btnAddNode = New System.Windows.Forms.Button()
  48.         Me.bthXPathSearches = New System.Windows.Forms.Button()
  49.         CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
  50.         Me.SuspendLayout()
  51.         '
  52.         'btnFill
  53.         '
  54.         Me.btnFill.Location = New System.Drawing.Point(8, 16)
  55.         Me.btnFill.Name = "btnFill"
  56.         Me.btnFill.Size = New System.Drawing.Size(104, 40)
  57.         Me.btnFill.TabIndex = 0
  58.         Me.btnFill.Text = "Fill the DataSet"
  59.         '
  60.         'DataGrid1
  61.         '
  62.         Me.DataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
  63.                     Or System.Windows.Forms.AnchorStyles.Right)
  64.         Me.DataGrid1.CaptionText = "Publishers and Titles"
  65.         Me.DataGrid1.DataMember = ""
  66.         Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
  67.         Me.DataGrid1.Location = New System.Drawing.Point(128, 16)
  68.         Me.DataGrid1.Name = "DataGrid1"
  69.         Me.DataGrid1.Size = New System.Drawing.Size(496, 184)
  70.         Me.DataGrid1.TabIndex = 1
  71.         '
  72.         'TreeView1
  73.         '
  74.         Me.TreeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  75.                     Or System.Windows.Forms.AnchorStyles.Left) _
  76.                     Or System.Windows.Forms.AnchorStyles.Right)
  77.         Me.TreeView1.ImageIndex = -1
  78.         Me.TreeView1.Location = New System.Drawing.Point(128, 216)
  79.         Me.TreeView1.Name = "TreeView1"
  80.         Me.TreeView1.SelectedImageIndex = -1
  81.         Me.TreeView1.Size = New System.Drawing.Size(496, 144)
  82.         Me.TreeView1.TabIndex = 2
  83.         '
  84.         'btnRefresh
  85.         '
  86.         Me.btnRefresh.Location = New System.Drawing.Point(8, 72)
  87.         Me.btnRefresh.Name = "btnRefresh"
  88.         Me.btnRefresh.Size = New System.Drawing.Size(104, 40)
  89.         Me.btnRefresh.TabIndex = 3
  90.         Me.btnRefresh.Text = "Refresh the TreeView"
  91.         '
  92.         'btnAddNode
  93.         '
  94.         Me.btnAddNode.Location = New System.Drawing.Point(8, 128)
  95.         Me.btnAddNode.Name = "btnAddNode"
  96.         Me.btnAddNode.Size = New System.Drawing.Size(104, 40)
  97.         Me.btnAddNode.TabIndex = 4
  98.         Me.btnAddNode.Text = "Add a new Node"
  99.         '
  100.         'bthXPathSearches
  101.         '
  102.         Me.bthXPathSearches.Location = New System.Drawing.Point(8, 184)
  103.         Me.bthXPathSearches.Name = "bthXPathSearches"
  104.         Me.bthXPathSearches.Size = New System.Drawing.Size(104, 40)
  105.         Me.bthXPathSearches.TabIndex = 5
  106.         Me.bthXPathSearches.Text = "XPath searches"
  107.         '
  108.         'XmlDataDocumentForm
  109.         '
  110.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  111.         Me.ClientSize = New System.Drawing.Size(632, 365)
  112.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bthXPathSearches, Me.btnAddNode, Me.btnRefresh, Me.TreeView1, Me.DataGrid1, Me.btnFill})
  113.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  114.         Me.Name = "XmlDataDocumentForm"
  115.         Me.Text = "XmlDataDocument Demo"
  116.         CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
  117.         Me.ResumeLayout(False)
  118.  
  119.     End Sub
  120.  
  121. #End Region
  122.  
  123.     Dim ds As New DataSet()
  124.     Dim xdd As XmlDataDocument
  125.  
  126.     ' fill a dataset
  127.  
  128.     Private Sub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click
  129.         Dim cn As New OleDbConnection(OledbPubsConnString)
  130.         Dim daPubs As New OleDbDataAdapter("SELECT * FROM Publishers", cn)
  131.         Dim daTitles As New OleDbDataAdapter("SELECT * FROM Titles", cn)
  132.         ' Fill the DataSet.
  133.         cn.Open()
  134.         daPubs.Fill(ds, "Publishers")
  135.         daTitles.Fill(ds, "Titles")
  136.         cn.Close()
  137.  
  138.         ' Create a (nested) relation between the two tables.
  139.         Dim rel As New DataRelation("PubsTitles", ds.Tables("Publishers").Columns("pub_id"), ds.Tables("Titles").Columns("pub_id"))
  140.         ds.Relations.Add(rel)
  141.         rel.Nested = True
  142.  
  143.         ' Associate the XmlDataDocument with the DataSet
  144.         xdd = New XmlDataDocument(ds)
  145.  
  146.         ' Bind the DataGrid 
  147.         DataGrid1.DataSource = ds
  148.         DataGrid1.DataMember = "Publishers"
  149.  
  150.         ' Fill the TreeView with the XmlDataDocument
  151.         DisplayXmlNode(xdd, TreeView1.Nodes)
  152.     End Sub
  153.  
  154.     ' refresh the display
  155.  
  156.     Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
  157.         TreeView1.Nodes.Clear()
  158.         DisplayXmlNode(xdd, TreeView1.Nodes)
  159.     End Sub
  160.  
  161.     ' add a node
  162.  
  163.     Private Sub btnAddNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNode.Click
  164.         ' Before proceding, you must set EnforceConstraints to False.
  165.         ds.EnforceConstraints = False
  166.         ' Add a new node to the XmlDataElement.
  167.         Dim xmlEl As XmlElement = CreateAppendElement(xdd.DocumentElement, "Publishers")
  168.         CreateAppendElement(xmlEl, "pub_id", "9997")
  169.         CreateAppendElement(xmlEl, "pub_name", "Sci-Fi Publications")
  170.         CreateAppendElement(xmlEl, "city", "Boston")
  171.         CreateAppendElement(xmlEl, "state", "MA")
  172.         CreateAppendElement(xmlEl, "country", "USA")
  173.         ' Normalizing after XML operations is a good habit.
  174.         xdd.Normalize()
  175.         ' Re-enable constraints.
  176.         ds.EnforceConstraints = True
  177.  
  178.         ' Display the result in the TreeView.
  179.         TreeView1.Nodes.Clear()
  180.         DisplayXmlNode(xdd, TreeView1.Nodes)
  181.     End Sub
  182.  
  183.     ' perform an XPath search
  184.  
  185.     Private Sub bthXPathSearches_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bthXPathSearches.Click
  186.         Dim xpath As String = "//Publishers[city = 'Boston']/Titles/title/text()"
  187.         Dim xnl As XmlNodeList = xdd.SelectNodes(xpath)
  188.         Dim xt As XmlText
  189.         Dim msg As String
  190.         For Each xt In xnl
  191.             msg &= xt.Value & ControlChars.CrLf
  192.         Next
  193.         MessageBox.Show(msg, "Result")
  194.  
  195.         xpath = "//Publishers[Titles/title/text() = 'Net Etiquette']"
  196.         Dim xmlEl As XmlElement = DirectCast(xdd.SelectSingleNode(xpath), XmlElement)
  197.         Dim dr As DataRow = xdd.GetRowFromElement(xmlEl)
  198.  
  199.         msg = dr("pub_name").ToString & ControlChars.CrLf
  200.         msg &= dr("city").ToString & ControlChars.CrLf
  201.         MessageBox.Show(msg, "Result")
  202.  
  203.     End Sub
  204. End Class
  205.